﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;

namespace CalendarWebpart {
    public class CalendarToolpart : ToolPart {
        DropDownList cboWeb = new DropDownList();
        DropDownList cboList = new DropDownList();
        DropDownList cboField = new DropDownList();
        DropDownList cboEndField = new DropDownList();
        DropDownList cboTooltipField = new DropDownList();
        DropDownList cboView = new DropDownList();

        public CalendarToolpart() {
            this.Init += new EventHandler(CalendarToolpart_Init);
            cboWeb.SelectedIndexChanged += new EventHandler(cboWeb_SelectedIndexChanged);
            cboList.SelectedIndexChanged += new EventHandler(cboList_SelectedIndexChanged);
            cboView.SelectedIndexChanged += new EventHandler(cboView_SelectedIndexChanged);
        }

        void cboWeb_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                EnsureChildControls();
                cboList.Items.Clear();
                cboField.Items.Clear();
                cboEndField.Items.Clear();
                cboTooltipField.Items.Clear();
                cboView.Items.Clear();
                using (SPWeb web = SPContext.Current.Site.OpenWeb(new Guid(cboWeb.SelectedValue)))
                {
                    foreach (SPList list in web.Lists)
                    {
                        cboList.Items.Add(list.Title);
                    }
                }
            }
            catch (Exception ex)
            {
                Page.Response.Write(ex.ToString());
            }
        }

        void cboList_SelectedIndexChanged(object sender, EventArgs e) {
            try {
                EnsureChildControls();
                cboField.Items.Clear();
                cboEndField.Items.Clear();
                cboTooltipField.Items.Clear();
                cboView.Items.Clear();
                using (SPWeb web = SPContext.Current.Site.OpenWeb(new Guid(cboWeb.SelectedValue)))
                {
                    SPList list = web.Lists[cboList.SelectedValue];
                    
                    cboView.Items.Add("");
                    foreach (SPView view in list.Views.OfType<SPView>().OrderBy(v => v.Title))
                    {
                        cboView.Items.Add(new ListItem(view.Title, view.ID.ToString("B").ToUpper()));
                    }

                    foreach (SPField field in list.Fields.OfType<SPField>().OrderBy(f => f.Title))
                    {
                        if (field.Type == SPFieldType.DateTime)
                        {
                            cboField.Items.Add(new ListItem(field.Title, field.InternalName));
                            cboEndField.Items.Add(new ListItem(field.Title, field.InternalName));
                        }
                        cboTooltipField.Items.Add(new ListItem(field.Title, field.InternalName));
                    }
                }
            } catch (Exception ex) {
                Page.Response.Write(ex.ToString());
            }
        }

        void cboView_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                EnsureChildControls();
                cboField.Items.Clear();
                cboEndField.Items.Clear();
                cboTooltipField.Items.Clear();
                using (SPWeb web = SPContext.Current.Site.OpenWeb(new Guid(cboWeb.SelectedValue)))
                {
                    SPList list = web.Lists[cboList.SelectedValue];

                    if (string.IsNullOrEmpty(cboView.SelectedValue))
                    {
                        foreach (SPField field in list.Fields.OfType<SPField>().OrderBy(f => f.Title))
                        {
                            if (field.Type == SPFieldType.DateTime)
                            {
                                cboField.Items.Add(new ListItem(field.Title, field.InternalName));
                                cboEndField.Items.Add(new ListItem(field.Title, field.InternalName));
                            }
                            cboTooltipField.Items.Add(new ListItem(field.Title, field.InternalName));
                        }
                    }
                    else
                    {
                        foreach (string fieldName in list.Views[new Guid(cboView.SelectedValue)].ViewFields.OfType<string>().OrderBy(f => f))
                        {
                            SPField field = list.Fields.GetFieldByInternalName(fieldName);
                            if (field.Type == SPFieldType.DateTime)
                            {
                                cboField.Items.Add(new ListItem(field.Title, field.InternalName));
                                cboEndField.Items.Add(new ListItem(field.Title, field.InternalName));
                            }
                            cboTooltipField.Items.Add(new ListItem(field.Title, field.InternalName));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Page.Response.Write(ex.ToString());
            }
        }

        void CalendarToolpart_Init(object sender, EventArgs e)
        {
            this.Title = "Calendar properties";
        }

        public override void ApplyChanges() {
            try {
                EnsureChildControls();
                CalendarWebpart wp = (CalendarWebpart)ParentToolPane.SelectedWebPart;
                wp.WebId = cboWeb.SelectedValue;
                wp.ListName = cboList.SelectedValue;
                wp.DateField = cboField.SelectedValue;
                wp.EndDateField = cboEndField.SelectedValue;
                wp.TooltipField = cboTooltipField.SelectedValue;
                wp.FilterView = cboView.SelectedValue;
            } catch (Exception ex) {
                Page.Response.Write(ex.ToString());
            }
        }

        protected override void CreateChildControls() {
            try {
                base.CreateChildControls();
                CalendarWebpart wp = (CalendarWebpart)ParentToolPane.SelectedWebPart;

                HtmlGenericControl cnt = new HtmlGenericControl();
                cnt.InnerHtml = "Select the web:<br />";
                Controls.Add(cnt);
                Controls.Add(cboWeb);
                cnt = new HtmlGenericControl();
                cnt.InnerHtml = "<br />Select the list:<br />";
                Controls.Add(cnt);
                Controls.Add(cboList);
                cnt = new HtmlGenericControl();
                cnt.InnerHtml = "<br />Select the filtering view:<br />";
                Controls.Add(cnt);
                Controls.Add(cboView);
                cnt = new HtmlGenericControl();
                cnt.InnerHtml = "<br />Select the date field:<br />";
                Controls.Add(cnt);
                Controls.Add(cboField);
                cnt = new HtmlGenericControl();
                cnt.InnerHtml = "<br />Select the end date field:<br />";
                Controls.Add(cnt);
                Controls.Add(cboEndField);
                cnt = new HtmlGenericControl();
                cnt.InnerHtml = "<br />Select the tooltip field:<br />";
                Controls.Add(cnt);
                Controls.Add(cboTooltipField);

                cboWeb.AutoPostBack = true;
                cboList.AutoPostBack = true;
                cboView.AutoPostBack = true;

                foreach (SPWeb web in SPContext.Current.Site.AllWebs)
                {
                    cboWeb.Items.Add(new ListItem(web.Title, web.ID.ToString()));
                }
                cboWeb.SelectedValue = wp.WebId;
                if (!string.IsNullOrEmpty(cboWeb.SelectedValue))
                {
                    using (SPWeb web = SPContext.Current.Site.OpenWeb(new Guid(cboWeb.SelectedValue)))
                    {
                        foreach (SPList list in web.Lists)
                        {
                            cboList.Items.Add(list.Title);
                        }
                        cboList.SelectedValue = wp.ListName;
                        if (!string.IsNullOrEmpty(cboList.SelectedValue))
                        {
                            SPList list = web.Lists[cboList.SelectedValue];

                            cboView.Items.Add("");
                            foreach (SPView view in list.Views.OfType<SPView>().OrderBy(v => v.Title))
                            {
                                cboView.Items.Add(new ListItem(view.Title, view.ID.ToString("B").ToUpper()));
                            }
                            cboView.SelectedValue = wp.FilterView;

                            if (string.IsNullOrEmpty(cboView.SelectedValue))
                            {
                                foreach (SPField field in list.Fields.OfType<SPField>().OrderBy(f => f.Title))
                                {
                                    if (field.Type == SPFieldType.DateTime)
                                    {
                                        cboField.Items.Add(new ListItem(field.Title, field.InternalName));
                                        cboEndField.Items.Add(new ListItem(field.Title, field.InternalName));
                                    }
                                    cboTooltipField.Items.Add(new ListItem(field.Title, field.InternalName));
                                }
                            }
                            else
                            {
                                foreach (string fieldName in list.Views[new Guid(cboView.SelectedValue)].ViewFields.OfType<string>().OrderBy(f => f))
                                {
                                    SPField field = list.Fields.GetFieldByInternalName(fieldName);
                                    if (field.Type == SPFieldType.DateTime)
                                    {
                                        cboField.Items.Add(new ListItem(field.Title, field.InternalName));
                                        cboEndField.Items.Add(new ListItem(field.Title, field.InternalName));
                                    }
                                    cboTooltipField.Items.Add(new ListItem(field.Title, field.InternalName));
                                }
                            }
                            cboField.SelectedValue = wp.DateField;
                            cboEndField.SelectedValue = wp.EndDateField;
                            cboTooltipField.SelectedValue = wp.TooltipField;
                        }
                    }
                }
            } catch (Exception ex) {
                HtmlGenericControl cnt = new HtmlGenericControl();
                cnt.InnerText = ex.ToString();
                Controls.Add(cnt);
            }
        }
    }
}
